<template>
  <IBox>
    <GenericCreateUpdateForm
      :fields="selectFields"
      :url="url"
      :initial="object"
      :update-success-next-route="successUrl"
      :clean-form-value="cleanFormValue"
      :object.sync="object"
      :fields-meta="fieldsMeta"
      :get-method="getMethod"
      :more-buttons="moreButtons"
    />
  </IBox>
</template>

<script>
import GenericCreateUpdateForm from '@/layout/components/GenericCreateUpdateForm'
import { testAlertEmailSetting } from '@/api/settings'
import { IBox } from '@/components'

export default {
  name: 'AlertEmail',
  components: {
    GenericCreateUpdateForm,
    IBox
  },
  props: {
    object: {
      type: Object,
      default: null
    }
  },
  data() {
    const vm = this
    return {
      selectFields: [[this.$t('common.BasicInfo'), ['ALERT_EMAIL_HOST', 'ALERT_EMAIL_PORT', 'ALERT_EMAIL_HOST_USER', 'ALERT_EMAIL_HOST_PASSWORD',
        'ALERT_EMAIL_FROM', 'ALERT_EMAIL_RECIPIENT']]],
      successUrl: { name: 'Settings', params: { activeMenu: 'AlertEmail' }},
      fieldsMeta: {
        ALERT_EMAIL_HOST: {
          label: this.$t('setting.emailHost'),
          rules: [
            { required: true, message: this.$t('common.fieldRequiredError') }
          ]
        },
        ALERT_EMAIL_PORT: {
          label: this.$t('setting.emailPort'),
          rules: [
            { required: true, message: this.$t('common.fieldRequiredError') }
          ]
        },
        ALERT_EMAIL_HOST_USER: {
          label: this.$t('setting.emailHostUser'),
          rules: [
            { required: true, message: this.$t('common.fieldRequiredError') }
          ]
        },
        ALERT_EMAIL_HOST_PASSWORD: {
          label: this.$t('setting.emailHostPassword'),
          helpText: this.$t('setting.helpText.emailHostPassword'),
          type: 'input',
          el: {
            type: 'password'
          }
        },
        ALERT_EMAIL_FROM: {
          label: this.$t('setting.emailEmailFrom'),
          helpText: this.$t('setting.helpText.emailEmailFrom')
        },
        ALERT_EMAIL_RECIPIENT: {
          label: this.$t('setting.emailRecipient'),
          helpText: this.$t('setting.helpText.emailRecipient')
        }
      },
      url: '/api/v1/settings/setting/',
      moreButtons: [
        {
          title: this.$t('setting.emailTest'),
          callback: function(value, form) {
            if (value['ALERT_EMAIL_HOST_PASSWORD'] === undefined) {
              value['ALERT_EMAIL_HOST_PASSWORD'] = ''
            }
            testAlertEmailSetting(value).then(res => {
              vm.$message.success(res['msg'])
            }).catch(res => {
              vm.$message.error(res['response']['data']['error'])
            })
          }
        }
      ]
    }
  },
  methods: {
    cleanFormValue(data) {
      if (data['ALERT_EMAIL_HOST_PASSWORD'] === '') {
        delete data['ALERT_EMAIL_HOST_PASSWORD']
      }
      return {
        alert_email: data
      }
    },
    getMethod() {
      return 'put'
    }
  }

}
</script>

<style scoped>

</style>
